package com.blog.controller.front;

import com.blog.aop.MethodTime;
import com.blog.aop.PvCount;
import com.blog.aop.PvTypeEnum;
import com.blog.aop.WeiboRequest;
import com.blog.configuration.MultiCache;
import com.blog.model.BaiduDto;
import com.blog.model.TestDto;
import com.blog.service.MainService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description: 主页controlelr
 * @Author: 刘会俊
 * @Date: 2018-11-29 14:16
 */
@Controller
public class MainController {

    private static final Logger log = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private MainService mainService;
    @Value("${code.line.count}")
    private String lineCount;

    /**
     * @description: 主页
     * @author: 刘会俊
     * @params: []
     * @return: java.lang.String
     */
    @MethodTime
    @PvCount(description = PvTypeEnum.HOME)
    @GetMapping(value = {"index", "/"})
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("html/index");
        List<Integer> countList = mainService.getIndexCount();
        mv.addObject("viewCount", countList.get(0));
        mv.addObject("peopleCount", countList.get(1));
        mv.addObject("coderLineCount", lineCount);
        return mv;
    }

    @ResponseBody
    @GetMapping("/weibo/feedback")
    public HashMap<String, Object> weiboFeedback(
        @WeiboRequest TestDto testDto
    ) throws IOException {
        if (log.isInfoEnabled()) {
            //log.info("[MainController.weiboFeedback] params[campaignid:{}, ip:{}, clicktime:{}, imei:{}, imp:{}]",
            //    campaignid, ip, clicktime, imei, imp);
            log.debug("[MainController.weiboFeedback] params[testDto:{}]", testDto.toString());
        }
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(
            "http://appmonitor.biz.weibo.com/sdkserver/active?company=aijiuyou&IMP"
                +
                "=E575BF2F8F39F1F062EFC5BA9E8744DAB730614DD5662C0A82C793A7AFEA3365C336F15E125B1BB61BDDF9123C6C77725678FB46259836835DECCBC26597D042")
            .build();
        Response response = okHttpClient.newCall(request).execute();
        log.info("[MainController.weiboFeedback]response = [{}]", response.body().string());
        return new HashMap<String, Object>() {
            private static final long serialVersionUID = -7174436155116711052L;

            {
            put("result", "ok");
            put("Code", "0");
        }};
    }

    @ResponseBody
    @GetMapping("/baidu/sem/activate")
    public String baiduFeedback(BaiduDto baiduDto, HttpServletRequest request){
        if(log.isInfoEnabled()){
            log.info("[MainController.baiduFeedback]参数为:[{}]", baiduDto == null ? null : baiduDto.toString());
        }
        log.info("[MainController.baiduFeedback]完整链接：[{}]", request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getServletPath()+"?"+request.getQueryString());
        return "success";
    }

    @Cacheable(cacheNames = MultiCache.NAME, key = "#a0")
    @GetMapping("/cache/{str}")
    @ResponseBody
    public String testCache(@PathVariable("str") String str){
        return str;
    }

}
